//
//  DetailFollowerCell.m
//  BigToBig
//
//  Created by liuyi on 7/20/16.
//  Copyright © 2016 cn.cloudtp. All rights reserved.
//

#import "DetailFollowerCell.h"
#import "FlowerCollectionCell.h"
#import "UsersNoticeModel.h"
#import "EntrepreneurDetailMessageController.h"

@interface DetailFollowerCell ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource
>

@property (nonatomic , strong)NSObject *selectModel;

@end

@implementation DetailFollowerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    UICollectionViewFlowLayout *followerLayout = [[UICollectionViewFlowLayout alloc]init];
    followerLayout.itemSize = CGSizeMake(kScreenWidth / 5, 100);
    followerLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    followerLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    self.followerCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake( 5,20, kScreenWidth, 100) collectionViewLayout:followerLayout];
    self.followerCollectionView.showsHorizontalScrollIndicator = NO;
    self.followerCollectionView.delegate = self;
    self.followerCollectionView.dataSource = self;
    self.followerCollectionView.backgroundColor = [UIColor whiteColor];
    [self.followerCollectionView registerClass:[FlowerCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FlowerCollectionCell class])];
    [self addSubview:self.followerCollectionView];

}

#pragma mark - followerCollectionView 代理方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FlowerCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FlowerCollectionCell class]) forIndexPath:indexPath];
    UsersNoticeModel *model = self.modelArray[indexPath.row];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://61.135.176.43:6080/data/HeadPortrait/%@",model.user_logo]] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    cell.nameLabel.text = model.name;
    return cell;
    
}

- (void)loadUserMessageModelWithUserId:(NSString *)userId UserType:(NSString *)userType{
    NSString *urlStr = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/user/show_by_id?id=%@",userId];
    NSDictionary *params = @{@"id":userId};
    [HttpTool textHTMLPostWithURL:urlStr params:params success:^(id json) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.selectModel = [UserMessageModel UserMessageModelWithDict:dict];
        NSLog(@"%@",self.selectModel);
        [self.followerCellDelegate pushToFollowerViewWithUserType:userType Model:(NSObject *)self.selectModel];

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UsersNoticeModel *userModel = self.modelArray[indexPath.row];
    switch (userModel.user_type.integerValue) {
        case 1:{
            [self loadUserMessageModelWithUserId:userModel.user_id UserType:userModel.user_type];
            break;
        }
        case 2:{
            [self loadUserMessageModelWithUserId:userModel.user_id UserType:userModel.user_type];
            break;
        }
            
        default:
            break;
    }

}
- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self.followerCollectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
